package com.boe.csb.core.test.service.csb;

import com.alibaba.csb.console.sdk.ConsoleHttpCaller;
import com.alibaba.csb.console.sdk.ConsoleSdkUtils;
import com.alibaba.csb.console.sdk.Result;
import com.alibaba.csb.console.sdk.param.JSONParamsBuilder;
import com.alibaba.csb.console.sdk.param.Parameter;
import com.alibaba.csb.console.sdk.param.ParamsBuilder;
import com.boe.csb.core.common.utils.GsonUtils;
import org.junit.Test;

/**
 * Created by tianjin.lp on 16/11/24.
 */
public class CsbConsoleApiExample {

    private static String svr = "http://115.28.111.194:8080";

    private static String queryCsbAPI = "api/admin/csbinstance/info";
    private static String queryUserAPI = "api/admin/user/info";
    private static String createCredentialAPI = "api/credentials/create";
    private static String queryProjectAPI = "api/project/get";

    private static String ak = "ebd8a57c56b244f8bbf80969c99bbc7a";
    private static String sk = "77iXwL3muI3d4q1zPMxYZMPiZVo=";

    @Test
    public void testCsbSdk() throws Exception {
        /**
         * 先找到csbId和userId,以后大部分接口都可能会需要这两个参数, 建议缓存起来
         */
        /**
         * 1. 找到csb名字对应的csb
         */
        ParamsBuilder builder = new ParamsBuilder();
        builder.append(new Parameter("csbName", "lt-qd-instance1"));

        String resultStr = ConsoleHttpCaller.doGet(svr, queryCsbAPI, builder.toString(), null, ak, sk);

        Result result = ConsoleSdkUtils.getCheckedResult(resultStr);

        String csbId = result.getId("csb");

        System.out.println(String.format("csb id is %s", csbId));
        /**
         * 2. (可选) 找到loginId对应的userId, 这里假设我们用相同的ak & sk,帮助别人来创建或者修改
         */
        builder.clear();
        builder.append(new Parameter("loginId", "wudiyihui@aliyun.com"));
        resultStr = ConsoleHttpCaller.doGet(svr, queryUserAPI, builder.toString(), null, ak, sk);
        result = ConsoleSdkUtils.getCheckedResult(resultStr);

        String userId = result.getArrayFirst("user", "userId");

        System.out.println(String.format("user id is %s", userId));
        /**
         * 3. (可选)可能需要查询project
         */
        builder.clear();
        builder.append(new Parameter("csbId", csbId))
                .append(new Parameter("projectName", "test1234123121113123123"))
                .append(new Parameter("userId", userId));

        String queryStr = builder.toString();

        resultStr = ConsoleHttpCaller.doGet(svr, queryProjectAPI, queryStr, null, ak, sk);

        result = ConsoleSdkUtils.getCheckedResult(resultStr);
        System.out.println(GsonUtils.object2String(result));

        /**
         * 4. 可以调用其他的接口, 比如创建凭证
         */
        builder.clear();
        builder.append(new Parameter("csbId", csbId)).append(new Parameter("userId", userId));
        queryStr = builder.toString();

        JSONParamsBuilder jsonParamBuilder = new JSONParamsBuilder();
        jsonParamBuilder.append("name", "testCreateCredential12dd4");

        builder.clear();
        builder.append(new Parameter("data", jsonParamBuilder.toJSONString()));
        String postStr = builder.toString();

        resultStr = ConsoleHttpCaller.doPost(svr, createCredentialAPI, queryStr, postStr, ak, sk);

        result = ConsoleSdkUtils.getCheckedResult(resultStr);
        System.out.println(GsonUtils.object2String(result));

    }
}
